package com.jinzhi.jzweb.controller;


import java.util.*;

import com.jinzhi.jzapi.domain.UserDO;
import com.jinzhi.jzapi.utils.JWTUtil;
import com.jinzhi.jzweb.dao.JazSignupUserDao;
import com.jinzhi.jzweb.domain.JazSignupUserDO;
import com.jinzhi.jzweb.domain.param.SignUserQuery;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.JazSignupTeamDO;
import com.jinzhi.jzweb.service.JazSignupTeamService;
import com.jinzhi.common.utils.Result;

import javax.servlet.http.HttpServletResponse;

/**
 * 
 * <pre>
 * 报名队伍表
 * </pre>
 * <small> 2024-09-05 15:21:58 | Long</small>
 */
@Controller
@RequestMapping("/jzweb/jazSignupTeam")
public class JazSignupTeamController extends BaseController {
	@Autowired
	private JazSignupTeamService jazSignupTeamService;
    @Autowired
    private JazSignupUserDao jazSignupUserDao;

	@GetMapping()
	@RequiresPermissions("jzweb:jazSignupTeam:jazSignupTeam")
	String JazSignupTeam(){
	    return "jzweb/jazSignupTeam/jazSignupTeam";
	}

	/**
	 * 分页查询队伍信息
	 * @param signUserQuery
	 * @return
	 */
	@ResponseBody
	@GetMapping("/list")
	public Result<Page<JazSignupTeamDO>> list(SignUserQuery signUserQuery){
        return Result.ok(jazSignupTeamService.selectTeamPage(signUserQuery));
	}


	/**
	 * 选手报名表导出
	 *
	 * @param response
	 */
	@GetMapping("/documentWord")
	@ResponseBody
	public void documentWord(HttpServletResponse response, JazSignupUserDO jazSignupUserDO) throws Exception {
		jazSignupTeamService.documentWord(response,jazSignupUserDO);
	}


	/**
	 * 根据id查询
	 * @param id
	 * @return
	 */
	@ResponseBody
	@GetMapping("/getById")
	public Result<JazSignupTeamDO> getById(Long id){
		return jazSignupTeamService.getById(id);
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@Transactional(rollbackFor = Exception.class)
	public Result<String>  remove( Long id){
		JazSignupTeamDO jazSignupTeamDO = jazSignupTeamService.selectById(id);
		if (jazSignupTeamDO==null){
			return Result.fail();
		}
		// 删除报名用户
		jazSignupUserDao.delete(new EntityWrapper<JazSignupUserDO>()
				.eq("team_id",jazSignupTeamDO.getId())
				.eq("group_id",jazSignupTeamDO.getGroupId()));
		// 删除报名队伍
		boolean b = jazSignupTeamService.deleteById(id);
		return b?Result.ok():Result.fail();
	}


	/**
	 * 队伍信息
	 * @return
	 */
	@GetMapping("/selTeamList")
	@ResponseBody
	public Result<?> selTeamList(@RequestParam("groupId")String groupId){
		EntityWrapper<JazSignupTeamDO> entityWrapper =new EntityWrapper();
		if (groupId!=null&&!groupId.equals("")) {
			entityWrapper.eq("group_id", groupId);
		}
		entityWrapper.orderBy("update_time",false);
		List<JazSignupTeamDO> list = jazSignupTeamService.selectList(entityWrapper);
		return Result.ok(list);
	}



	/**
	 * 导出报名信息
	 * @param response
	 * @param signUserQuery
	 */
	//@Log(value = "导出系统日志",sysModule = 12,operationType = 4)
	@ResponseBody
	@GetMapping("/download")
	public void downloadImportTemplate(HttpServletResponse response, SignUserQuery signUserQuery) {
		 jazSignupTeamService.downloadImportTemplate(response, signUserQuery);
	}

	
}
